package com.yiqi.liebang.feature.setting.view;

import android.content.Intent;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.TextView;

import com.luck.picture.lib.tools.PictureFileUtils;
import com.suozhang.framework.framework.AM;
import com.suozhang.framework.framework.BaseActivity;
import com.suozhang.framework.utils.T;
import com.suozhang.framework.utils.logger.Logger;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;
import com.yiqi.liebang.R;
import com.yiqi.liebang.common.rongim.SealAppContext;
import com.yiqi.liebang.common.util.CacheManager;
import com.yiqi.liebang.common.widget.ShareBottomDialog;
import com.yiqi.liebang.common.widget.dialog.BaseAnimatorSet;
import com.yiqi.liebang.common.widget.dialog.BounceTopEnter;
import com.yiqi.liebang.common.widget.dialog.NormalDialog;
import com.yiqi.liebang.common.widget.dialog.OnBtnClickL;
import com.yiqi.liebang.common.widget.dialog.SlideBottomExit;

import butterknife.BindView;
import butterknife.OnClick;
import cn.jpush.android.api.JPushInterface;

public class SettingActivity extends BaseActivity implements CompoundButton.OnCheckedChangeListener {


    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.sw_notifation)
    Switch mSwNotifation;
    @BindView(R.id.tv_setting_clean)
    TextView mTvSettingClean;
    NormalDialog normalDialog;
    UMImage image;//网络图片
    UMWeb umWeb;

     ShareBottomDialog mShareDialog;
    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_setting;
    }

    @Override
    protected void initInjector() {

    }


    @Override
    public void initView() {
        mSwNotifation.setChecked(AM.user().isAllowPush()?true:false);

        image = new UMImage(this, R.drawable.ic_launcher);
        umWeb = new UMWeb("http://liebangapp.com/yindao/");
        umWeb.setTitle("「猎帮」互联网知识分享平台，一个帮你赚钱的知识分享APP");//标题
        umWeb.setThumb(image);  //缩略图
        umWeb.setDescription("诚邀您加入猎帮");//描述


        normalDialog = new NormalDialog(this);
        normalDialog
                .content("是否确定退出登录?")//
                .showAnim(new BounceTopEnter())//
                .dismissAnim(new SlideBottomExit());
        normalDialog.setOnBtnClickL(
                new OnBtnClickL() {
                    @Override
                    public void onBtnClick() {
                        try{
                            if (normalDialog != null) {
                                normalDialog.superDismiss();
                            }
                        }catch (Exception e){
                            e.printStackTrace();
                        }
                    }
                },
                new OnBtnClickL() {
                    @Override
                    public void onBtnClick() {
                        AM.user().loginOut();
                        SealAppContext.getInstance().logout();
                        try{
                            if (normalDialog != null) {
                                normalDialog.superDismiss();
                            }
                        }catch (Exception e){
                            e.printStackTrace();
                        }
                        finish();
                    }
                });
        initToolBar(mToolbar, "设置", true, true);
    }

    @Override
    protected void initData() {
        try {
            mTvSettingClean.setText(CacheManager.getTotalCacheSize(this));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void initEvent() {
        mSwNotifation.setOnCheckedChangeListener(this);
    }

    @OnClick({R.id.tv_setting_about, R.id.tv_setting_ys, R.id.tv_setting_invite, R.id.tv_setting_account, R.id.tv_setting_clean, R.id.tv_setting_logut})
    public void onViewClicked(View view) {
        switch (view.getId()) {

            case R.id.tv_setting_about:
                startActivity(new Intent(this, AboutActivity.class));
                break;
            case R.id.tv_setting_ys:
                startActivity(new Intent(this, PrivacyPolicyActivity.class));
                break;
            case R.id.tv_setting_invite:
                BaseAnimatorSet mBasIn = new BounceTopEnter();
                mShareDialog = new ShareBottomDialog(this, null,true);
                mShareDialog.setWxCircleClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        new ShareAction(SettingActivity.this)
                                .setPlatform(SHARE_MEDIA.WEIXIN_CIRCLE)//传入平台
                                .withMedia(umWeb)
                                .setCallback(shareListener)//回调监听器
                                .share();
                    }
                });

                mShareDialog.setWxClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        new ShareAction(SettingActivity.this)
                                .setPlatform(SHARE_MEDIA.WEIXIN)//传入平台
                                .withMedia(umWeb)
                                .setCallback(shareListener)//回调监听器
                                .share();
                    }
                });
                mShareDialog.showAnim(mBasIn)
//                            .dismissAnim()
                        .show();//
                break;
            case R.id.tv_setting_account:
                startActivity(new Intent(this, AccountSecurityActivity.class));
                break;
            case R.id.tv_setting_clean:
                CacheManager.clearAllCache(this);
                PictureFileUtils.deleteCacheDirFile(this);
                try {
                    T.showShort("清除成功!");
                    String mDatas = CacheManager.getTotalCacheSize(this);
                    if (!TextUtils.isEmpty(mDatas)) {
                        mTvSettingClean.setText(mDatas);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case R.id.tv_setting_logut:

                if (normalDialog != null) {
                    normalDialog.show();
                }

//
                break;
        }
    }
    private UMShareListener shareListener = new UMShareListener() {
        @Override
        public void onStart(SHARE_MEDIA platform) {
            showLoading();
        }

        @Override
        public void onResult(SHARE_MEDIA platform) {
            dismissLoading();
            T.showShort("分享成功！");
            if (mShareDialog != null) mShareDialog.dismiss();
        }

        @Override
        public void onError(SHARE_MEDIA platform, Throwable t) {
            dismissLoading();
            T.showShort("分享失败！"+t.getMessage());
            if (mShareDialog != null) mShareDialog.dismiss();
        }

        @Override
        public void onCancel(SHARE_MEDIA platform) {
            dismissLoading();
            T.showShort("分享取消！");

        }
    };
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        Logger.e("启用锁屏开锁isChecked = " + isChecked);

        if (isChecked && !AM.user().isLogin()) {
            buttonView.setChecked(false);
            return;
        }
        AM.user().setAllowPush(isChecked);
        if (AM.user().isAllowPush()){
            JPushInterface.resumePush(this);
        }else {
            JPushInterface.stopPush(this);
        }

    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        try{
            if (normalDialog != null) {
                normalDialog.superDismiss();
            }
        }catch (Exception e){
            e.printStackTrace();
        }

    }
}